package com.you.rpc.until;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class HessianUtil {

    /**
     * Hessian序列化
     */
    public static byte[] hessianSerialize(Object object) {
        Hessian2Output oo = null;
        byte[] result = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oo = new Hessian2Output(bos);
            oo.writeObject(object);
            oo.flush();
            result = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Hessian反序列化
     */
    public static Object hessianSerializeToObj(byte[] bytes) {
        Object result = null;
        try {
            ByteArrayInputStream is = new ByteArrayInputStream(bytes);
            Hessian2Input input = new Hessian2Input(is);
            result = input.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
